﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CERT.Services.DAL;
using CERT.Services;
using System.Web.Mvc;


namespace CERT.Controllers
{
    public class IncidentDetailController : ApiController
    {
       private INETServices Service;


       public IncidentDetailController()
       {
           Service = new NETServices();

       }

       public IncidentDetailController(INETServices service)
        {
            Service = service;

        }

        

        // GET api/Incident/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public Incident GetIncident(int id)
        {
            if (id <= 0)
            {
               var newIncident = new Incident();
               newIncident.DateOfIndicent = DateTime.Now;
               return newIncident;
            }
            
                Incident incident = Service.GetSingleIncident(a => a.Id == id);
            

            if (incident == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return incident;
        }

        // PUT api/Incident/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public HttpResponseMessage PutIncident(int id, Incident team)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

  
            try
            {
                Service.UpdateIncident(team, id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Incident
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
       public HttpResponseMessage PostIncident(Incident incident)
        {
           

            try
            {
                //create new team and send in.
                Service.AddIncident(incident);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            var response = Request.CreateResponse(HttpStatusCode.Created, incident);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = incident.Id }));
                return response;
           
        }

        // DELETE api/Incident/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public System.Net.Http.HttpResponseMessage DeleteIncident(int id)
        {
           
            if (id == 0)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
            
          

            try
            {
                Service.DeleteIncident(id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, id);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}